import tornado.ioloop
import tornado.web
from tornado.options import options
# import tornado.web.RequestHandler as RequestHandler
import os, sys
import datetime
from PyWebsite3.fund_manager_handler import RenderManagerPageHandler
from PyWebsite3.mutualfund_handler import RenderMutualFundPageHandler
from PyWebsite3.top_manager_handler import RenderTopManagerHandler

# 设置模板路径
template_path = os.path.join(os.path.dirname(__file__), "templates_element")
static_path = os.path.join(os.path.dirname(__file__), "static")

settings = {"template_path": template_path, "static_path": static_path}


def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
        (r'/manager/(.*)', RenderManagerPageHandler),
        (r'/mutualfund/(.*)', RenderMutualFundPageHandler),
        (r'/top_manager/(.*)', RenderTopManagerHandler),
        (r"/testData/", TestDataHandler),
        (r"/testTable", TestTableHandler),
        (r"/testPic", TestPictureHandler),
        (r"/testECharts", TestEChartsHandler),
        (r"/testExtend", TestExtendHandler),
    ], **settings)


class My404Handler(tornado.web.RequestHandler):
    # Override prepare() instead of get() to cover all possible HTTP methods.
    def prepare(self):
        self.set_status(404)
        self.render("404.html")


class TestPictureHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("test_pic.html")


class TestEChartsHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("test_echarts.html")


class TestExtendHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("test_extend.html")


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        items = ["Item 1", "Item 2", "Item 3"]
        self.render("test_button.html")


class TestDataHandler(tornado.web.RequestHandler):
    def get(self):
        category = self.get_argument('page')
        authorname = self.get_argument('limit')

        items = []
        items.append({"id": 1, "username": "aaa"})
        items.append({"id": 2, "username": "bbb"})
        test_json = {"data": items}
        test_json["code"] = 0

        self.write(test_json)


class TestTableHandler(tornado.web.RequestHandler):
    def get(self):
        items = ["Item 1", "Item 2", "Item 3"]
        self.render("test_pages.html", items=items)


if __name__ == "__main__":

    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

